package com.visikard.ui.adapter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.visikard.application.Constant;
import com.visikard.application.UIMessage;
import com.visikard.item.ItemTransactionHistory;
import com.visikard.ui.R;
import com.visikard.utility.BitmapUtils;
import com.visikard.utility.UILImageUtils;
import com.visikard.utility.Utils;

public class TransactionHistoryGiftAdapter extends BaseAdapter {

	Context context;
	List<ItemTransactionHistory> data;
	LayoutInflater inflater;

	DateFormat formatter;
	DateFormat dateOnlyFormatter;
	DateFormat timeFormat;
	Handler mHandler;
	DisplayMetrics metrics;

	public TransactionHistoryGiftAdapter(Context ct,
			List<ItemTransactionHistory> dt, Handler mHandler) {
		this.context = ct;
		this.data = dt;
		this.mHandler = mHandler;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		formatter = new SimpleDateFormat(Constant.TIME_FORMAT_STR);
		dateOnlyFormatter = new SimpleDateFormat(Constant.TIME_FORMAT_STR_MONTH_DATE);
		timeFormat = new SimpleDateFormat(Constant.TIME_FULL_FORMAT_STR, Locale.US);
		metrics = context.getResources().getDisplayMetrics();
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		if (position < data.size()) {
			return data.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder;

		if (convertView == null) {
			convertView = inflater.inflate(
					R.layout.item_transaction_history_gift_kard, null);
//			Utils.overrideFonts(convertView);
			viewHolder = new ViewHolder();
			
			viewHolder.lyItemUser = (RelativeLayout) convertView.findViewById(R.id.item_transaction_user);
			viewHolder.lyHeader = (RelativeLayout) convertView.findViewById(R.id.item_transaction_header);
			
			viewHolder.mTextHeader = (TextView) convertView.findViewById(R.id.transaction_text_header);
			viewHolder.mTimeZone = (TextView) convertView.findViewById(R.id.text_transaction_timezone);
			viewHolder.mTextNo = (TextView) convertView.findViewById(R.id.transaction_text_header_no);
			viewHolder.mUsername = (TextView) convertView.findViewById(R.id.text_transaction_username);
			viewHolder.mGkValue = (TextView) convertView.findViewById(R.id.text_transaction_gkvalue);
			viewHolder.mRedeeme = (TextView) convertView.findViewById(R.id.text_transaction_redemptionvalue);
			viewHolder.mCurrent = (TextView) convertView.findViewById(R.id.text_transaction_currentvalue);
			
			viewHolder.imgKard = (ImageView) convertView.findViewById(R.id.image_kard);
			viewHolder.imgGiftKard = (ImageView) convertView.findViewById(R.id.image_kard_gift_kard);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		ItemTransactionHistory item = (ItemTransactionHistory) getItem(position);
		if (item != null) {

			int mWidth = metrics.widthPixels / 4;
			int mHeight = mWidth * 3 / 2;

			RelativeLayout.LayoutParams lyKard = (RelativeLayout.LayoutParams) viewHolder.imgKard
					.getLayoutParams();
			int kardHeight = mWidth * 2 / 3;
			lyKard.width = kardHeight * 2 / 3;
			lyKard.height = kardHeight;

			RelativeLayout.LayoutParams lyGiftKard = (RelativeLayout.LayoutParams) viewHolder.imgGiftKard
					.getLayoutParams();
			lyGiftKard.width = mHeight;
			lyGiftKard.height = mWidth;

			viewHolder.mUsername.setText(item.getUsername());
			if (!item.getKardImage().isEmpty())
//				ImageLoader.getInstance(context).loadImageCorner(
//						viewHolder.imgKard, item.getKardImage(),
//						Device.getDeviceWidth() / 4, BitmapUtils.ROUND_ALL, 4,
//						3, R.drawable.kard_shadow_default_horizontal, 0, 0);
				UILImageUtils.loadImage(viewHolder.imgKard, //image view
						item.getKardImage(), //url
						1, //size of view
						BitmapUtils.ROUND_ALL, //round all 
						4, // round size
						3, //shadow
						R.drawable.kard_shadow_default_horizontal, //shadow resource 
						0, 0); 

			if (!item.getImageGiftKard().isEmpty())
//				ImageLoader.getInstance(context).loadImageCorner(
//						viewHolder.imgGiftKard, item.getImageGiftKard(),
//						Device.getDeviceWidth() / 3, BitmapUtils.ROUND_ALL, 4,
//						3, R.drawable.kard_shadow_default_horizontal, 0, 0);
				UILImageUtils.loadImage(viewHolder.imgGiftKard, //image view
						item.getImageGiftKard(), //url
						1, //size of view
						BitmapUtils.ROUND_ALL, //round all 
						4, // round size
						3, //shadow
						R.drawable.kard_shadow_default_horizontal, //shadow resource 
						0, 0); 
			try {
//				if(item.getIscoupon() == 0 && item.getIsGiftSpecial() == 0)
//					viewHolder.mGkValue.setText("$" + String.format("%.2f", Double.parseDouble(item.getOriginalAmount())));
//				else
//					viewHolder.mGkValue.setText(item.getOriginalAmount());
//				if (item.getIscoupon() == 0)
//					viewHolder.mRedeeme.setText("$" + String.format("%.2f", Double.parseDouble(item.getRedemptionValue())));
//				else
//					viewHolder.mRedeeme.setText(item.getRedemptionValue());
				
				viewHolder.mGkValue.setText("$" + String.format("%.2f", item.getOriginalAmountNumber()));
				viewHolder.mRedeeme.setText("$" + String.format("%.2f", Double.parseDouble(item.getRedemptionValue())));
				
				viewHolder.mCurrent.setText("$" + String.format("%.2f", Double.parseDouble(item.getRemainingValue())));
			} catch (Exception e) {
				e.printStackTrace();
			}

			try {
//				Date today = dateOnlyFormatter.parse(dateOnlyFormatter
//						.format(new Date()));
//				Date date = dateOnlyFormatter
//						.parse(dateOnlyFormatter.format(formatter.parse(item
//								.getDateTimeWithTimezone())));
				Date today = new Date();
				Date date = new Date(item.getDateTime());

				if (position < 1) {
					if (Utils.compareDateOnly(date, today) == 0) {
						String strDate = context.getResources().getString(R.string.literal_today); 
						viewHolder.mTextHeader.setText(strDate);
					}else{
						Calendar cal = Calendar.getInstance();
						cal.setTime(date);
						viewHolder.mTextHeader.setText(dateOnlyFormatter.format(date));
					}
					viewHolder.lyHeader.setVisibility(View.VISIBLE);
				} else {
//					Date date_above = dateOnlyFormatter
//					.parse(dateOnlyFormatter.format(formatter
//							.parse(data.get(position - 1)
//									.getDateTimeWithTimezone())));
					Date date_above = new Date(data.get(position - 1).getDateTime());
					if (Utils.compareDateOnly(date, date_above) != 0) {
						Calendar cal = Calendar.getInstance();
						cal.setTime(date);
						viewHolder.mTextHeader.setText(dateOnlyFormatter.format(date));
						viewHolder.lyHeader.setVisibility(View.VISIBLE);
					} else
						viewHolder.lyHeader.setVisibility(View.GONE);
				}
				viewHolder.mTimeZone.setText(timeFormat.format(formatter.parse(item.getDateTimeWithTimezone())));
			} catch (Exception e) {
				e.printStackTrace();
			}
			viewHolder.lyItemUser.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Message msg = new Message();
					msg.what = UIMessage.MSG_OPEN_TRASACTIONS_DEATIL;
					msg.arg1 = position;
					mHandler.sendMessage(msg);
				}
			});
		}
		return convertView;
	}

	private class ViewHolder {
		TextView mUsername, mCurrent, mRedeeme, mGkValue, mTextHeader, mTimeZone, mTextNo;
		ImageView imgKard, imgGiftKard;
		RelativeLayout lyItemUser, lyHeader;
	}

}
